#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.dirname(File.realpath($PROGRAM_NAME))))

require "#{LKP_SRC}/lib/statistics"

wakeupstats = Hash.new { |h, k| h[k] = [] }
wakeup_end_latency = Hash.new { |h, k| h[k] = [] }
requeststats = Hash.new { |h, k| h[k] = [] }
request_end_latency = Hash.new { |h, k| h[k] = [] }

counter = -1
in_wakeup_section = 0
in_request_section = 0

$stdin.each_line do |line|
  case line
  when /^Iteration/
    counter += 1
  when /^Wakeup Latencies percentiles/
    in_wakeup_section = 1
    in_request_section = 0
  when /^Request Latencies percentiles/
    in_wakeup_section = 0
    in_request_section = 1
  when /^RPS percentiles/
    in_wakeup_section = 0
    in_request_section = 0
  when /50.0th/
    if in_wakeup_section == 1
      wakeupstats['latency_50%_us'] << line.split[1].to_i
      wakeup_end_latency['latency_50%_us'][counter] = line.split[1].to_i
    elsif in_request_section == 1
      requeststats['latency_50%_us'] << line.split[1].to_i
      request_end_latency['latency_50%_us'][counter] = line.split[1].to_i
    end
  when /90.0th/
    if in_wakeup_section == 1
      wakeupstats['latency_90%_us'] << line.split[1].to_i
      wakeup_end_latency['latency_90%_us'][counter] = line.split[1].to_i
    elsif in_request_section == 1
      requeststats['latency_90%_us'] << line.split[1].to_i
      request_end_latency['latency_90%_us'][counter] = line.split[1].to_i
    end
  when /99.0th/
    if in_wakeup_section == 1
      wakeupstats['latency_99%_us'] << line.split[2].to_i
      wakeup_end_latency['latency_99%_us'][counter] = line.split[2].to_i
    elsif in_request_section == 1
      requeststats['latency_99%_us'] << line.split[2].to_i
      request_end_latency['latency_99%_us'][counter] = line.split[2].to_i
    end
  when /99.9th/
    if in_wakeup_section == 1
      wakeupstats['latency_99.9%_us'] << line.split[1].to_i
      wakeup_end_latency['latency_99.9%_us'][counter] = line.split[1].to_i
    elsif in_request_section == 1
      requeststats['latency_99.9%_us'] << line.split[1].to_i
      request_end_latency['latency_99.9%_us'][counter] = line.split[1].to_i
    end
  when /min/
    if in_wakeup_section == 1
      line = line.gsub(/([,=])/, ' ')
      wakeupstats["latency_#{line.split[0]}_us"] << line.split[1].to_i
      wakeupstats["latency_#{line.split[2]}_us"] << line.split[3].to_i
    elsif in_request_section == 1
      line = line.gsub(/([,=])/, ' ')
      requeststats["latency_#{line.split[0]}_us"] << line.split[1].to_i
      requeststats["latency_#{line.split[2]}_us"] << line.split[3].to_i
    end
  end
end
wakeupstats.each do |k, v|
  puts "wakeup_#{k}: #{v.average.round(2)}"
  puts "wakeup_#{k}_stddev%: #{v.relative_stddev.round(2)}"
end

wakeup_end_latency.each do |k, v|
  puts "wakeup_end_#{k}_max: #{v.max}"
  puts "wakeup_end_#{k}_stddev%: #{v.relative_stddev.round(2)}"
end

requeststats.each do |k, v|
  puts "request_#{k}: #{v.average.round(2)}"
  puts "request_#{k}_stddev%: #{v.relative_stddev.round(2)}"
end

request_end_latency.each do |k, v|
  puts "request_end_#{k}_max: #{v.max}"
  puts "request_end_#{k}_stddev%: #{v.relative_stddev.round(2)}"
end
